﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace GreateTool
{
    public partial class PdfDataCheckForm : Form
    {
        public PdfDataCheckForm()
        {
            InitializeComponent();
        }

        Dictionary<string, string> _dic;

        private void button1_Click(object sender, EventArgs e)
        {
            _dic = new Dictionary<string, string>();

            openFileDialog1.InitialDirectory = "c:\\";

            if (openFileDialog1.ShowDialog() != DialogResult.OK) return;

            var str = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><root>" +
                System.IO.File.ReadAllText(openFileDialog1.FileName) + "</root>";

            var sr = new System.IO.StringReader(str);

            var xr = new XmlTextReader(sr);

            while (xr.Read())
            {
                if (xr.Depth == 0 || xr.NodeType != XmlNodeType.Element) continue;

                if (xr.Name == "file") _dic[xr[0]] = xr.ReadOuterXml();
            }

            xr.Close();
            sr.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string xml;

            dataGridView1.Rows.Clear();

            if (sender != null)
            {
                openFileDialog1.FileName = null;
                openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                if (openFileDialog1.ShowDialog() != DialogResult.OK) return;
                textBox1.Text = System.IO.Path.GetDirectoryName(openFileDialog1.FileName);
                textBox2.Text = System.IO.Path.GetFileName(openFileDialog1.FileName);
            }

            if (!_dic.TryGetValue(textBox2.Text, out xml)) return;

            var sr = new System.IO.StringReader("<?xml version=\"1.0\" encoding=\"utf-8\" ?><root>" + xml + "</root>");

            var xr = new XmlTextReader(sr);

            while (xr.Read())
            {
                if (xr.Depth == 0 || xr.NodeType != XmlNodeType.Element) continue;

                if (xr.Name == "file") ReadFile(xr.ReadSubtree());

                xr.Skip();
            }

            xr.Close();
            sr.Close();

            try
            {
                var file = System.IO.Path.Combine(textBox1.Text, textBox2.Text.Insert(textBox2.Text.Length - 4, "_bookmark"));
                System.Diagnostics.Process.Start(file);
            }
            catch
            {
                var file = System.IO.Path.Combine(textBox1.Text, textBox2.Text);
                System.Diagnostics.Process.Start(textBox1.Text);
            }
        }

        void ReadFile(XmlReader xr)
        {
            xr.Read();

            while (xr.Read())
            {
                if (xr.NodeType != XmlNodeType.Element) continue;

                ReadTable(xr.ReadSubtree());

                xr.Skip();
            }

            xr.Close();
        }

        void ReadTable(XmlReader xr)
        {
            xr.Read();

            dataGridView1.Rows.Add(xr.Name);

            while (xr.Read())
            {
                if (xr.NodeType != XmlNodeType.Element) continue;

                var arr = new string[7];

                ReadData(xr.ReadSubtree(), arr);

                dataGridView1.Rows.Add(arr);

                xr.Skip();
            }

            xr.Close();
        }

        void ReadData(XmlReader xr, string[] arr)
        {
            xr.Read();

            arr[1] = xr.Name;

            while (xr.Read())
            {
                if (xr.NodeType != XmlNodeType.Element) continue;

                switch (xr.Name)
                {
                    case "des": arr[2] = xr.ReadString(); break;
                    case "upd": arr[3] = xr.ReadString(); break;
                    case "cla": arr[4] = xr.ReadString(); break;
                    case "usp": arr[5] = xr.ReadString(); break;
                    case "csp": arr[6] = xr.ReadString(); break;
                }

                xr.Skip();
            }

            xr.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            openFileDialog1.InitialDirectory = "c:\\";

            if (openFileDialog1.ShowDialog() != DialogResult.OK) return;

            System.Diagnostics.Process.Start(openFileDialog1.FileName);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            TextLinesInputForm frm = new TextLinesInputForm();

            if (frm.ShowDialog() == DialogResult.OK)
            {
                Array.ForEach<string>(frm.Lines, n => listView1.Items.Add(n));
            }
        }

        private void listView1_ItemActivate(object sender, EventArgs e)
        {
            var item = listView1.SelectedItems[0];
            if (string.IsNullOrEmpty(item.Text)) return;
            textBox2.Text = item.Text;
            item.BackColor = System.Drawing.Color.Yellow;
            button2_Click(null, EventArgs.Empty);
        }
    }
}
